home *** CD-ROM | disk | FTP | other *** search
/ Ian & Stuart's Australian Mac 1993 September / September 93.iso / Archives / Utilities / Print / Printer Utilities / DW Print MacPaint / print MacPaint.c < prev   
C/C++ Source or Header  |  1992-12-03  |  6KB  |  304 lines

  1. /*
  2.     MacPaint printing utility
  3.     © 1992, S. Wilkerson
  4.  
  5.     written with THINK C 5.0
  6. */
  7.  
  8. #include <PrintTraps.h>
  9.  
  10. /* params */
  11.  
  12. #define abtDialog 128
  13. #define alrtDialog 128
  14.  
  15. enum    {
  16.     deskAcc = 1,
  17.     file,
  18.     edit,
  19.     about = 1,
  20.     pagesetup = 1,
  21.     print,
  22.     quit = 4,
  23.     undo = 1,
  24.     cut = 3,
  25.     copy,
  26.     paste,
  27.     clear
  28. };
  29.  
  30. MenuHandle deskAccM, fileM, editM, viewM;
  31. THPrint hPrint;
  32.  
  33. main()
  34. {
  35.     Boolean notDone = true, DoMenus();
  36.     EventRecord event;
  37.     WindowPtr whichWindow;
  38.  
  39.     Init();
  40.     FlushEvents( everyEvent, 0 );
  41.     while( notDone )    {
  42.         if ( WaitNextEvent( everyEvent, &event, 60L, NIL ) )    {
  43.             switch ( event.what )    {
  44.  
  45.                 case osEvt:
  46.                 /* !! need to check this out */
  47.                 break;
  48.  
  49.                 case mouseDown:
  50.                 switch( FindWindow( event.where, &whichWindow ) )    {
  51.  
  52.                     case inMenuBar:
  53.                     notDone = DoMenus( MenuSelect( event.where ) );
  54.                     break;
  55.  
  56.                     case inSysWindow:
  57.                     SystemClick( &event, whichWindow );
  58.                     break;
  59.  
  60.                     default:
  61.                     break;
  62.                 }
  63.                 break;
  64.  
  65.                 case keyDown:
  66.                 case autoKey:
  67.                 if ( event.modifiers & cmdKey )
  68.                     notDone = DoMenus( MenuKey( (char)event.message ) );
  69.                 break;
  70.  
  71.                 default:
  72.                 break;
  73.             }
  74.         }
  75.     }
  76.     Cleanup();
  77.  
  78. }    /* main */
  79.  
  80. Boolean DoMenus( select )
  81. long select;
  82. {
  83.     short menuID, menuItem;
  84.     Boolean result = true;
  85.     Str255 theAcc;
  86.  
  87.     menuItem = LoWord( select );
  88.     switch( menuID = HiWord( select ) )    {
  89.  
  90.         case deskAcc:
  91.         if ( menuItem == about )
  92.             OpenAbout();
  93.         else    {
  94.             GetItem( deskAccM, menuItem, theAcc );
  95.             OpenDeskAcc( theAcc );
  96.         }
  97.         break;
  98.  
  99.         case file:
  100.         switch( menuItem )    {
  101.  
  102.             case pagesetup:
  103.             PrStlDialog( hPrint );
  104.             break;
  105.  
  106.             case print:
  107.             PrintMacPaintFile();
  108.             break;
  109.  
  110.             case quit:
  111.             result = false;
  112.             break;
  113.  
  114.             default:
  115.             break;
  116.         }
  117.         break;
  118.  
  119.         case edit:
  120.         SystemEdit( menuItem - 1 );
  121.         break;
  122.  
  123.         default:
  124.         break;
  125.     }
  126.     HiliteMenu( 0 );
  127.     return( result );
  128.  
  129. }    /* DoMenus */
  130.  
  131. pascal void myItem( theDialog, itemNo )
  132. DialogPtr theDialog;
  133. short itemNo;
  134. {
  135.     short itemType;
  136.     Handle item;
  137.     Rect displayRect;
  138.  
  139.     GetDItem( theDialog, itemNo, &itemType, &item, &displayRect );
  140.     PenSize( 3, 3 );
  141.     InsetRect( &displayRect, -4, -4 );
  142.     FrameRoundRect( &displayRect, 16, 16 );
  143.     PenNormal();
  144.  
  145. }    /* myItem */
  146.  
  147. OpenAbout()
  148. {
  149.     DialogPtr theDialog;
  150.     short itemHit = 0, itemType;
  151.     Handle item;
  152.     Rect box;
  153.     pascal void myItem();
  154.  
  155.     theDialog = GetNewDialog( abtDialog, NIL, (WindowPtr)-1 );
  156.     GetDItem( theDialog, OK, &itemType, &item, &box );
  157.     SetDItem( theDialog, OK + 1, userItem, (Handle)myItem, &box );
  158.     while ( !itemHit )
  159.         ModalDialog( NIL, &itemHit );
  160.     DisposDialog( theDialog );
  161.  
  162. }    /* OpenAbout */
  163.  
  164. Boolean GetFile( fileName, vRefNum, numTypes, reqTypes )
  165. char *fileName;
  166. short *vRefNum, numTypes;
  167. SFTypeList reqTypes;
  168. {
  169.     short i;
  170.     Point TopLeft;
  171.     SFTypeList typeList;
  172.     SFReply reply;
  173.  
  174.     SetPt( &TopLeft, 80, 90 );
  175.     SFGetFile( TopLeft, "\p", 0L, numTypes, reqTypes, 0L, &reply );
  176.     if ( reply.good )    {
  177.         for ( i = 0; i <= reply.fName[0]; i++ )
  178.             fileName[i] = reply.fName[i];
  179.         *vRefNum = reply.vRefNum;
  180.         return ( true );
  181.     }
  182.     else
  183.         return ( false );
  184.  
  185. }    /* GetFile */
  186.  
  187. PrintMacPaintFile()
  188. {
  189.     Str255 fileName;
  190.     short vRefNum, refNum;
  191.     long reqTypes = 'PNTG';
  192.     Rect theRect;
  193.     WindowPtr theTempWindow;
  194.     TPPrPort pPrPort;
  195.     TPPrint pPrint;
  196.     TPrStatus prStatus;
  197.     Boolean spoolNeeded, GetFile();
  198.  
  199.     if ( GetFile( fileName, &vRefNum, 1, &reqTypes ) )    {
  200.         if ( FSOpen( fileName, vRefNum, &refNum ) == noErr )    {
  201.             if ( PrJobDialog( hPrint ) && ( PrError() == noErr ) )    {
  202.                 SetCursor( *GetCursor( watchCursor ) );
  203.                 SetRect( &theRect, 1, 1, 2, 2 );
  204.                 theTempWindow = NewWindow( NIL, &theRect, fileName, true,
  205.                     plainDBox, (WindowPtr)-1L, false, 1L );
  206.                 PrValidate( hPrint );
  207.                 pPrPort = PrOpenDoc( hPrint, NIL, NIL );
  208.                 PrOpenPage( pPrPort, NIL );
  209.                 DrawMacPaintFile( refNum );
  210.                 PrClosePage( pPrPort );
  211.                 PrCloseDoc( pPrPort );
  212.                 HLock( hPrint );
  213.                 pPrint = *hPrint;
  214.                 spoolNeeded = ( pPrint->prJob.bJDocLoop == bSpoolLoop );
  215.                 HUnlock( hPrint );
  216.                 if ( spoolNeeded && ( PrError() == noErr ) )
  217.                     PrPicFile( hPrint, NIL, NIL, NIL, &prStatus );
  218.                 DisposeWindow( theTempWindow );
  219.                 InitCursor();
  220.             }
  221.         }
  222.         else    {
  223.             StopAlert( alrtDialog, NIL );
  224.         }
  225.         FSClose( refNum );
  226.     }
  227.  
  228. }    /* PrintMacPaintFile */
  229.  
  230. DrawMacPaintFile( refNum )
  231. short refNum;
  232. {
  233.     short i;
  234.     long logEOF;
  235.     Ptr srcPtr, dstPtr;
  236.     BitMap theBitMap;
  237.     Rect theRect;
  238.  
  239.     GetEOF( refNum, &logEOF );
  240.     logEOF -= 512L;
  241.     SetFPos( refNum, fsFromStart, 512L );
  242.     srcPtr = NewPtr( logEOF );
  243.     FSRead( refNum, &logEOF, srcPtr );
  244.     theBitMap.baseAddr = NewPtr( 72L );
  245.     theBitMap.rowBytes = 72;
  246.     SetRect( &(theBitMap.bounds), 0, 0, 576, 1 );
  247.     SetRect( &theRect, 0, 0, 576, 1 );
  248.     for ( i = 0; i < 720; i++ )    {
  249.         dstPtr = theBitMap.baseAddr;
  250.         UnpackBits( &srcPtr, &dstPtr, 72 );
  251.         CopyBits( &theBitMap, &(thePort->portBits), &(theBitMap.bounds),
  252.             &theRect, srcCopy, NIL );
  253.         OffsetRect( &theRect, 0, 1 );
  254.     }
  255.     DisposPtr( theBitMap.baseAddr );
  256.     DisposPtr( srcPtr );
  257.  
  258. }    /* DrawMacPaintFile */
  259.  
  260. Cleanup()
  261. {
  262.     DisposeHandle( hPrint );
  263.     PrClose();
  264.  
  265. }    /* Cleanup */
  266.  
  267. Init()
  268. {
  269.     Rect theRect;
  270.     char apple[2];
  271.  
  272. /* managers */
  273.  
  274.     InitGraf( &thePort );
  275.     InitFonts();
  276.     InitWindows();
  277.     InitDialogs( NIL );
  278.     InitMenus();
  279.     MaxApplZone();
  280.  
  281. /* menus */
  282.  
  283.     apple[0] = 1; apple[1] = 0x14;
  284.     deskAccM = NewMenu( deskAcc, apple );
  285.     AppendMenu( deskAccM, "\pAbout this thing…;-(" );
  286.     AddResMenu( deskAccM, 'DRVR' );
  287.     InsertMenu( deskAccM, 0 );
  288.     fileM = NewMenu( file, "\pFile" );
  289.     AppendMenu( fileM, "\pPage Setup…;Print…/P;-(;Quit/Q" );
  290.     InsertMenu( fileM, 0 );
  291.     editM = NewMenu( edit, "\pEdit" );
  292.     AppendMenu( editM, "\pUndo/Z;-(;Cut/X;Copy/C;Paste/V;Clear" );
  293.     InsertMenu( editM, 0 );
  294.     DrawMenuBar();
  295.  
  296. /* misc */
  297.  
  298.     PrOpen();
  299.     hPrint = (THPrint)NewHandle( sizeof( TPrint ) );
  300.     PrintDefault( hPrint );
  301.     InitCursor();
  302.  
  303. }    /* Init */
  304.